package com.xinyi.reactivemvvm.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.xinyi.httpdemo.config.OkHttpConfig;
import com.xinyi.reactivemvvm.model.NeteaseCommentModel;
import com.xinyi.reactivemvvm.model.Resource;
import com.xinyi.reactivemvvm.model.request.NeteaseCommentRequest;
import com.xinyi.reactivemvvm.sources.repository.MainRepository;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

/**
 * @author 杨耿雷
 * @date 2024/9/23 22:31
 * @description 主页ViewModel
 */
@HiltViewModel
public class MainViewModel extends ViewModel {

    /**
     * 主页数据仓库
     */
    private final MainRepository mMainRepository;

    /**
     * 网抑云音乐热评数据
     */
    private final MutableLiveData<Resource<NeteaseCommentModel>> mNeteaseComment;

    public MutableLiveData<Resource<NeteaseCommentModel>> getNeteaseComment() {
        return mNeteaseComment;
    }

    @Inject
    public MainViewModel(MainRepository mainRepository) {
        this.mMainRepository = mainRepository;
        mNeteaseComment = new MutableLiveData<>();
    }

    /**
     * 随机获取一条网抑云音乐热评
     * @param request 请求参数
     */
    public void requestNeteaseComment(NeteaseCommentRequest request) {
        // 通过okhttp线程池管理器获取线程池执行任务 TODO 若是在kotlin中可直接采用ViewModel协程作用域
        OkHttpConfig.getInstance().getClient().dispatcher().executorService().execute(() -> {
            mMainRepository.getNeteaseComment(request).collect(mNeteaseComment::postValue);
        });
    }
}
